package com.typy.www.tpay.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.typy.www.tpay.R;

/**
 * Created by 13799 on 2018/6/17.
 */

public class LineGridView extends GridView {
    private int rownum;

    public LineGridView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public LineGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LineGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //通过重新dispatchTouchEvent方法来禁止滑动
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            return true;//禁止Gridview进行滑动
        }
        return super.dispatchTouchEvent(ev);
    }

    private int mHeight = 0;

    public void setmHeight(int i_hei) {
        mHeight = i_hei;
    }

    //恢复默认  等待进行重新计算
    public void revertDefault() {
        mHeight = 0;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //在这里进行重新计算 做了判断 避免重复的多余的计算
        if (mHeight == 0 && getChildCount() > 0) {
            fixChildHei();
        }


        int expandSpec;
        if (mHeight > 0) {
            expandSpec = MeasureSpec.makeMeasureSpec(mHeight,
                    MeasureSpec.EXACTLY);
        } else {
            expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int colnum = getNumColumns(); //获取列数
        int total = getChildCount();  //获取Item总数
        //计算行数
        if (total % colnum == 0) {
            rownum = total / colnum;
        } else {
            rownum = (total / colnum) + 1; //当余数不为0时，要把结果加上1
        }
        Paint localPaint; //设置画笔
        localPaint = new Paint();
        localPaint.setStyle(Paint.Style.STROKE); //画笔实心
        localPaint.setColor(getContext().getResources().getColor(R.color.divider_color));//画笔颜色

        View view0 = getChildAt(0); //第一个view
        View viewColLast = getChildAt(colnum - 1);//第一行最后一个view
        View viewRowLast = getChildAt((rownum - 1) * colnum); //第一列最后一个view


        for (int i = 1, c = 1; i < rownum || c < colnum; i++, c++) {
            //画横线
            canvas.drawLine(view0.getLeft(), view0.getBottom() * i, viewColLast.getRight(), viewColLast.getBottom() * i, localPaint);
            //画竖线
            canvas.drawLine(view0.getRight() * c, view0.getTop(), viewRowLast.getRight() * c, viewRowLast.getBottom(), localPaint);
        }
    }

    private void fixChildHei() {
        final int PRE_GRIDVIEW_HEI = getHeight();
        final int CHILD_COUNT = getChildCount();
        if (CHILD_COUNT > 0) {
            final int NUMCOLUMN = getNumColumns();
            for (int i_index = 0; i_index < CHILD_COUNT / NUMCOLUMN; i_index++) {
                int t_heimax = 0;
                //先取得maxhei
                for (int i_colum = 0; i_colum < NUMCOLUMN; i_colum++) {
                    if (getChildAt(i_index * NUMCOLUMN + i_colum).getMeasuredHeight() > t_heimax) {
                        t_heimax = getChildAt(i_index * NUMCOLUMN + i_colum).getMeasuredHeight();
                    }
                }

                final int MAXHEI = t_heimax;
                //再修正height有问题的 view
                for (int i_colum = 0; i_colum < NUMCOLUMN; i_colum++) {
                    if (getChildAt(i_index * NUMCOLUMN + i_colum).getMeasuredHeight() != MAXHEI) {
                        ViewGroup.LayoutParams layoutParams = getChildAt(i_index * NUMCOLUMN + i_colum).getLayoutParams();
                        layoutParams.height = MAXHEI;
                        getChildAt(i_index * NUMCOLUMN + i_colum).setLayoutParams(layoutParams);
                    }
                }
            }
            //求得gridview自身的高度
            int thishei = getChildAt(0).getMeasuredHeight() * CHILD_COUNT / NUMCOLUMN +
                    getPaddingBottom() +
                    getPaddingTop();
            //计算VerticalSpacing
            if (Build.VERSION.SDK_INT >= 16) {
                thishei += (CHILD_COUNT / NUMCOLUMN - 1) * getVerticalSpacing();
            }
            if (PRE_GRIDVIEW_HEI > thishei) {
                setmHeight(thishei);
                Log.d(getClass().getSimpleName(), "fixCHildHei");
            }
        }
    }
}
